package com.dns.reset.web.service;

import com.dns.reset.web.model.DbDnsEntity;
import com.dns.reset.web.repository.DbDnsRepository;
import com.dns.reset.web.repository.DbTelnetRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;


@Service
public class DbDnsService {
    @Autowired
    private DbDnsRepository dbDnsRepository;
    @Autowired
    private DbTelnetRepository dbTelnetRepository;

    private AtomicInteger integer = new AtomicInteger();

    public void initAtomicInteger(Integer num){
        integer.set(num);
    }
    public Integer getAtomicInteger(){
        return integer.incrementAndGet(); //先+1,然后在返回值,相当于++i
    }
    public Integer findMaxNo(){
        return dbDnsRepository.findMaxNo();
    }

    public DbDnsEntity save(DbDnsEntity dbDnsEntity){
        return dbDnsRepository.save(dbDnsEntity);
    }
    @Transactional
    public void initStart(){
        dbDnsRepository.updateIsNew();
        Calendar ca=Calendar.getInstance();
        ca.setTime(new Date());
        ca.add(Calendar.HOUR_OF_DAY, 1);
        dbTelnetRepository.updateIsExistAndDestroyTime(ca.getTime());
    }
    public List<DbDnsEntity> findByIsNew(Integer isNew){
        return dbDnsRepository.findByIsNew(isNew);
    }

    public Page<DbDnsEntity> queryForPage(int page , int size) {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        Pageable pageable = new PageRequest(page-1, size, sort);
        return dbDnsRepository.findAll(pageable);
    }
}
